package com.jeecms.wicool.dao.car.impl;


import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Finder;
import com.jeecms.common.hibernate3.HibernateBaseDao;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.car.CarSeriesDao;
import com.jeecms.wicool.entity.car.CarSeries;

@Repository
public class CarSeriesDaoImpl extends HibernateBaseDao<CarSeries, Integer>
		implements CarSeriesDao {
	HttpServletRequest request;
	@Override
	protected Class<CarSeries> getEntityClass() {
		// TODO Auto-generated method stub
		return CarSeries.class;
	}
	@Override
	public Pagination getPage(String seriesName,String shortName,Integer carBrandId,int pageNo, int pageSize,CmsUser user) {
			Finder f = Finder.create("select bean from CarSeries bean where 1=1 ");
			if (!StringUtils.isBlank(seriesName)){
				f.append(" and bean.seriesName like :seriesName");
				f.setParam("seriesName", "%" + seriesName + "%");
			}if (!StringUtils.isBlank(shortName)){
				f.append(" and bean.shortName like :shortName");
				f.setParam("shortName", "%" + shortName + "%");
			}if (null!=carBrandId){
				f.append(" and bean.carBrand.id = :carBrandId");
				f.setParam("carBrandId",carBrandId);
			}
			if(user.getShopId()!=null){
				f.append(" and bean.shop.id = :shopId");
				f.setParam("shopId",user.getShopId());
			}
			f.append(" order by bean.orderNum desc");
			return find(f, pageNo, pageSize);
	}
	@Override
	public CarSeries findById(Integer id) {
		CarSeries entity = get(id);
		return entity;
	}
	@Override
	public CarSeries save(CarSeries bean) {
		getSession().save(bean);
		return bean;
	}
	@Override
	public CarSeries deleteById(Integer id) {
		CarSeries entity = super.get(id);
		if (entity != null) {
			getSession().delete(entity);
		}
		return entity;
	}
	@Override
	public List<CarSeries> getAll(CmsUser user) {
		Finder f= Finder.create("select bean from CarSeries bean where 1=1 ");
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
	@Override
	public List<CarSeries> getCarSeriesByCarBrand(Integer carBrandId,CmsUser user){
		
		Finder f = Finder.create("from CarSeries bean where 1=1 ");
		if(carBrandId!=null){
			f.append(" and bean.carBrand.id = :carBrandId") ;
			f.setParam("carBrandId",carBrandId);
		}
		if(user.getShopId()!=null){
			f.append(" and bean.shop.id = :shopId");
			f.setParam("shopId",user.getShopId());
		}
		f.append(" order by bean.id desc");
		return find(f);
	}
}
